﻿using System.Collections.Generic;
using CKS.EBE.Logging;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
using Microsoft.SharePoint.Utilities;

namespace CKS.EBE.API
{
	internal static class Localization
	{
		private const string DefaultResourceFile = "CKSEBE";
		private const string CoreResourceFile = "core";

		public static IList<string> DocumentLibraries = new List<string>(new[] { "Media", "Photos", "Themes", "Binaries" });
		public enum BlogList { Posts, Comments, Categories, OtherBlogs, Links, Photos, Media, Themes, Binaries }
		public enum FieldNames { Categories }

		// static, because it won't change
		internal static SharePointVersion SPVersion;

		// Original lists resources' keys
		//private const string PostsListName = "posts_schema_blg_title";
		private const string PostsFolderName = "blogpost_Folder";
		//private const string BloglistListName = "bloglist_schema_blg_title";
		private const string BloglistFolderName = "bloglist_Folder";
		//private const string CommentsListName = "comments_schema_blg_title";
		private const string CommentsFolderName = "blogcomments_Folder";
		//private const string LinksListName = "links_schema_blg_title";
		private const string LinksFolderName = "links_Folder";
		//private const string CategoriesListName = "categories_schema_blg_title";
		private const string CategoriesFolderName = "blogcatagories_Folder";
		// CKS lists resources' keys
		private const string ThemesListName = "ListName_Themes";
		private const string MediaListName = "ListName_Media";
		private const string BinariesListName = "ListName_Binaries";
		private static readonly Dictionary<string, string> ListNames = new Dictionary<string, string>();

		static Localization()
		{
			if (SPVersion == SharePointVersion.Invalid)
			{
				// read the SharePoint version with elevated privilegues from the farm
				SPSecurity.RunWithElevatedPrivileges(() =>
				{
					if (SPFarm.Local.BuildVersion.Major == 15)
						SPVersion = SharePointVersion.SharePoint2013;
					else
						SPVersion = SharePointVersion.Older;
				});
			}

		}

		/// <summary>
		/// returns the RootFolder name for the list (e.g. Posts)
		/// </summary>
		/// <param name="list"></param>
		/// <param name="language"></param>
		/// <returns></returns>
		public static string GetListName(BlogList list, uint language)
		{
			string res = list.ToString();
			string dictionaryKey = string.Format("{0}{1}", res, language);
			// if the dictionary already contains the listname, don't use resources to find it
			if (ListNames.ContainsKey(dictionaryKey)) return ListNames[dictionaryKey];

			switch (list)
			{
				case BlogList.Posts:
					res = GetResource(PostsFolderName, CoreResourceFile, language);
					break;
				case BlogList.Comments:
					res = GetResource(CommentsFolderName, CoreResourceFile, language);
					break;
				case BlogList.Categories:
					res = GetResource(CategoriesFolderName, CoreResourceFile, language);
					break;
				case BlogList.OtherBlogs:
					res = GetResource(BloglistFolderName, CoreResourceFile, language);
					break;
				case BlogList.Links:
					res = GetResource(LinksFolderName, CoreResourceFile, language);
					break;
				case BlogList.Themes:
					res = GetResource(ThemesListName, DefaultResourceFile, language);
					break;
				case BlogList.Binaries:
					res = GetResource(BinariesListName, DefaultResourceFile, language);
					break;
				case BlogList.Media:
					res = GetResource(MediaListName, DefaultResourceFile, language);
					break;
			}
			// add key to dictionary
			ListNames.Add(dictionaryKey, res);

			return res;
		}

		public static string GetFieldName(FieldNames field, uint language)
		{
			switch (field)
			{
				case FieldNames.Categories:
					return SPVersion == SharePointVersion.Older ? "Categories" : "PostCategory";
				default:
					return field.ToString();
			}
		}

		public static string GetResource(string resKey, string resFile, uint language)
		{
			string localizedString = SPUtility.GetLocalizedString("$Resources:" + resKey, resFile, language);
			if (localizedString.StartsWith("$Resources"))
			{
				Logger.Error(new SPException("Cannot find a resource '" + resKey + "' in resourcefile '" + resFile + "'"), "Localization.GetResource");
			}
			return localizedString;
		}

		public static string GetResource(string resKey, string resFile)
		{
			// get language from web regionalsettings
			uint language = SPContext.Current.Web.RegionalSettings.LocaleId;
			return GetResource(resKey, resFile, language);
		}

		public static string GetResource(string resKey, uint language)
		{
			return GetResource(resKey, DefaultResourceFile, language);
		}

		public static string GetResource(string resKey)
		{
			// get language from web regionalsettings
			uint languageID = SPContext.Current.Web.RegionalSettings.LocaleId;
			return GetResource(resKey, DefaultResourceFile, languageID);
		}
	}
}
